<?php
/**
 * Created by PhpStorm.
 * User: m1514
 * Date: 2016/12/29
 * Time: 17:03
 */

namespace Common\Model;


class CommodityModel extends BaseModel
{
    protected $pk='gid';
    protected $tableName='goods';
    protected $_validate=[
        array('gname','require','请填写商品名称'),
        array('gnumber','require','请填写商品货号'),
        array('unit','require','请填写商品单位'),
        array('marketprice','require','请填写商品市场价格'),
        array('shopprice','require','请填写商品商城价格'),
        array('click','require','请填写商品点击次数'),
    ];
    //添加
    public function addshop()
    {
        //1.商品表验证
        if (!$this->create()) {
            return ['status' => 'faild', 'message' => $this->getError()];
            die;
        }
        //2.商品详情表验证
        $detailModel = new DetailModel();
        if (!$detailModel->create()) {
            return ['status' => 'faild', 'message' => $detailModel->getError()];
            die;
        }
        if (!isset($_POST ['detailPic'])) {
            return ['status' => 'faild', 'message' => '请上传商品图册'];
            die;
        }
        $commodityAttrModel = new CommodityAttrModel();
//        if(!$commodityAttrModel->create()){
//            return ['status' => 'faild', 'message' => $detailModel->getError()];
//            die;
//        }

//—————————————————————————————————————————————————————————————————————————————————
        //1.商品表添加

        $data = I('post.');
        $data['time'] = time();
        $data['aid'] = $_SESSION['admin']['aid'];
        $gid = $this->add($data);
        //2.商品详情表添加处理
        //2.1大中小图处理
        $image = new \Think\Image();
        $small = '';
        $medium = '';
        $big = '';
        foreach ($_POST['detailPic'] as $k => $v) {
            $image->open($v);
            //2.2处理路径
            $smallPath = dirname($v) . '/small_' . basename($v);
            $mediumPath = dirname($v) . '/medium_' . basename($v);
            $bigPath = dirname($v) . '/big_' . basename($v);
            //2.3图片缩略
            $image->thumb(800, 800)->save($bigPath);
            $image->thumb(400, 400)->save($mediumPath);
            $image->thumb(80, 80)->save($smallPath);
            //2.4获得路径
            $small .= $smallPath . ',';
            $medium .= $mediumPath . ',';
            $big .= $bigPath . ',';
        }
        //2.5执行添加
        //2.6在post提交过来的数据中压入数据表中对应的字段
        $data['gid'] = $gid;
        $data['small'] = rtrim($small, ',');//rtrim去掉一整条数据最后面的，号；
        $data['medium'] = rtrim($medium, ',');
        $data['big'] = rtrim($big, ',');
        $detailModel->add($data);
        //3商品属性添加

        //属性添加
        foreach ($data['attr'] as $k => $v) {
            if ($v) {
                $data = ['gtvalue' => $v, 'added' => 0, 'taid' => $k, 'gid' => $gid];
                $commodityAttrModel->add($data);
            }
        }
        //规格添加
        foreach ($_POST['spec'] as $k => $v) {
            foreach ($v['tavalue'] as $kk => $vv) {
                $data = ['gtvalue' => $vv, 'added' => $v['added'][$kk], 'taid' => $k, 'gid' => $gid];
                $commodityAttrModel->add($data);
            }
        }
        return ['status' => 'success', 'message' => '操作成功'];
    }
    //商品编辑
    public function edit(){

        //1.商品表验证
        if (!$this->create()) {
            return ['status' => 'faild', 'message' => $this->getError()];
            die;
        }
        //2.商品详情表验证
        $detailModel = new DetailModel();
        if (!$detailModel->create()) {
            return ['status' => 'faild', 'message' => $detailModel->getError()];
            die;
        }
        if (!isset($_POST ['detailPic'])) {
            return ['status' => 'faild', 'message' => '请上传商品图册'];
            die;
        }
        $commodityAttrModel = new CommodityAttrModel();
//        if(!$commodityAttrModel->create()){
//            return ['status' => 'faild', 'message' => $detailModel->getError()];
//            die;
//        }

//————————————————————————————————————————————————————————————————————————————————————
        //商品表修改
        $post=I('post.');
        $gid=I('get.gid');
        $post['time'] = time();
        $post['aid'] = $_SESSION['admin']['aid'];
        $this->where("gid={$gid}")->save($post);
//        dd($post);die;

        //大中小图处理
        $image = new \Think\Image();
        $small = '';
        $medium = '';
        $big = '';
        foreach ($_POST['detailPic'] as $k => $v) {
            $image->open($v);
            //2.2处理路径
            $smallPath = dirname($v) . '/small_' . basename($v);
            $mediumPath = dirname($v) . '/medium_' . basename($v);
            $bigPath = dirname($v) . '/big_' . basename($v);
            //2.3图片缩略
            $image->thumb(800, 800)->save($bigPath);
            $image->thumb(400, 400)->save($mediumPath);
            $image->thumb(80, 80)->save($smallPath);
            //2.4获得路径
            $small .= $smallPath . ',';
            $medium .= $mediumPath . ',';
            $big .= $bigPath . ',';
        }
        //2.6在post提交过来的数据中压入数据表中对应的字段
        $post['gid'] = $gid;
        $post['small'] = rtrim($small, ',');//rtrim去掉一整条数据最后面的，号；
        $post['medium'] = rtrim($medium, ',');
        $post['big'] = rtrim($big, ',');
//        dd($post);die;
        //商品详细表修改操作
        $detailModel->where("gid={$gid}")->save($post);

        //3.属性，规格添加
        //先删除原来的
        $commodityAttrModel->where("gid={$gid}")->delete();
        //在执行添加，来实现修改数据

        foreach ($post['attr'] as $k => $v) {
            if ($v) {
                $post = ['gtvalue' => $v, 'added' => 0, 'taid' => $k, 'gid' => $gid];
                $commodityAttrModel->add($post);
            }
        }
        //规格添加
        foreach ($_POST['spec'] as $k => $v) {
            foreach ($v['tavalue'] as $kk => $vv) {
                $data = ['gtvalue' => $vv, 'added' => $v['added'][$kk], 'taid' => $k, 'gid' => $gid];
                $commodityAttrModel->add($data);
            }
        }

        return ['status' => 'success', 'message' => '操作成功'];
    }
    //商品删除
    public function del(){
        $gid=I('get.gid');
//        dd($gid);die;
        //商品表
        $aa=$this->where("gid={$gid}")->delete();
        //商品详细表删除操作
        $detailModel = new DetailModel();
        $bb=$detailModel->where("gid={$gid}")->delete();
        //商品属性表删除操作
        $commodityAttrModel = new CommodityAttrModel();
        $cc=$commodityAttrModel->where("gid={$gid}")->delete();
//        dd($cc);die;



        return ['status' => 'success', 'message' => '操作成功'];
    }

}